﻿
using FAMailUtils.MailGun;
using RestSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;

namespace FAGateway
{
    public partial class frmCheckMail : Form
    {
        DomainUtils utils = new DomainUtils("key-0hvxsu9bubvdc9060do5rn44z3masyw0");
        SendMail sendmail = new SendMail("key-0hvxsu9bubvdc9060do5rn44z3masyw0");
        public frmCheckMail()
        {
            InitializeComponent();
        }
        string accessKey, secretKey, password, server, username;
        private void btnEmailVerify_Click(object sender, EventArgs e)
        {
            //getConfigAmazone();
            ////VerifyEmail veriryEmail = new VerifyEmail(accessKey, secretKey);
            //bool status = EmailTools.IsEmail(txtEmail.Text.Trim());
            //MessageBox.Show(status + "");

            EmailTools eTool = new EmailTools();
            if (EmailTools.IsEmail(txtEmail.Text))
            {
                ArrayList arr = EmailTools.FindDomains(txtEmail.Text);
                DomainList list = utils.GetDomains();
                var result = list.items.Select(a => a.name = arr[0] + "");
                if (result.Count() > 0)
                {
                    utils.AddDomain(arr[0] + "", txtSMTPPass.Text);
                }
            }
        }

        private void getConfigAmazone()
        {
            accessKey = ConfigurationManager.AppSettings["AccessKey"].ToString();
            if (accessKey == null)
            {
                accessKey = "AKIAISOKS5VTXBRTVFEQ";
            }
            secretKey = ConfigurationManager.AppSettings["SecrectKey"].ToString();
            if (secretKey == null)
            {
                secretKey = "4vWG7GoCd5JbZ/r9hxA8MTIoo9elkkvRxW80qyU6";
            }

            password = ConfigurationManager.AppSettings["PasswordSMTP"].ToString();
            if (password == null)
            {
                password = "AjzPf6o0UITv4qtyXEzXY15xm9nmyVhb7Tk7g2X8DOPk";
            }

            server = ConfigurationManager.AppSettings["ServerSMTP"].ToString();
            if (server == null)
            {
                server = "email-smtp.us-east-1.amazonaws.com";
            }

            username = ConfigurationManager.AppSettings["UserNameSMTP"].ToString();
            if (username == null)
            {
                username = "AKIAJGQEDHANI2RZVAWQ";
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string email = txtSubject.Text;
            try
            {
                MailAddress m = new MailAddress(email);
                // Khong kiem tra voi yahoo mail.
                if (!m.Host.ToLower().Equals("yahoo.com")
                    && !m.Host.ToLower().Equals("yahoo.com.vn"))
                {
                    string text = "Scan: " + email;
                    //logs_scan.Info(text); //lblInfo.Text = text;
                    EmailVerifier verify = new EmailVerifier(true);
                    bool rs = verify.CheckExists(email);
                    if (!rs)
                    {
                        // Xoa va sao luu khach hang nay.
                        //deleteAndBackupCustomer(row);
                        text = "Scan-Fail: " + email;
                        txtEmail.Text = text;
                        //logs_scan.Info(text); //lblInfo.Text = text;
                    }
                    else
                    {
                        text = "Scan-OK: " + email;
                        //logs_scan.Info(text); //lblInfo.Text = text;
                        txtEmail.Text = text;
                    }
                }
            }
            catch (Exception ex)
            {
                // Xoa va sao luu khach hang nay.
                //deleteAndBackupCustomer(row);
                //logs_scan.Info("Scan-Error: " + email, ex);
            }

        }



        private void btnSend_Click(object sender, EventArgs e)
        {
            List<string> to = new List<string>();
            to.Add(txtDomain.Text);
            SendResult result = sendmail.SendSimpleMessage(EmailTools.FindDomains(txtEmail.Text)[0] + "", "Phong Gmail", txtEmail.Text, to, txtSubject.Text, txtBody.Text);
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            var result = sendmail.GetLogsTracking(txtDomain.Text.Split('@')[1],1);
            DomainList list = utils.GetDomains(true);
            txtDomainVerified.Text = "Count: " + list.total_count;
            foreach (var item in list.items)
            {
                txtDomainVerified.Text += Environment.NewLine + "Domain: " + item.name + Environment.NewLine;
                foreach (var obj in item.detail.sending_dns_records)
                    txtDomainVerified.Text += "name: " + obj.value+", valid"+obj.valid;
            }
        }

        private void txtValidate_Click(object sender, EventArgs e)
        {

            EmailValidate emailValidate = new EmailValidate("pubkey-4yw49kkqw9r7gfx0rv9q22c2dxcwbxr9");
            EmailValidateResult result = emailValidate.GetValidate(txtDomain.Text);
            if (result.is_valid) txtBody.Text = "OK";
            else txtBody.Text = "Fail" + Environment.NewLine + result.did_you_mean;
        }

        private void btnGetStats_Click(object sender, EventArgs e)
        {
            //StatsResult result = sendmail.GetStats(txtEmail.Text.Split('@')[1] + "");
            //foreach (var item in result.items)
            //{

            //}

            Logs temp = sendmail.GetLogs(txtEmail.Text.Split('@')[1], 1);

            if (temp.isGetOK)
            {
                string content = "";
                foreach (var item in temp.items)
                {
                    content += "to: " + item.to;
                    content += Environment.NewLine;
                    content += "message-id: " + item.messageId;
                    content += Environment.NewLine;
                    content += "timestamp: " + item.timestamp;
                    content += Environment.NewLine;
                    content += "event: " + item.Event;
                    content += Environment.NewLine;
                    content += Environment.NewLine;
                }
                txtBody.Text = content;
            }
            var result = temp.items.Where(a => a.Event == "opened");
            if (!string.IsNullOrEmpty(temp.paging.next))
            {
                temp = sendmail.GetLogNextPage(temp.paging.next, txtEmail.Text.Split('@')[1]);
            }
        }

        
    }
}
